package csxXiti.xiti4;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @author WuYongheng
 * @date 2021/11/24
 * @description
 */
public class xiti4_1 {
    public static List<Double> spiltRedPackets(double money, int count) {
        List list = new ArrayList();
        // 设置最大红包金额为  平均值的 2 倍
        double max = money / count * 2;
        // 判断界限  bound就是界限
        double bound=0.0;
        for (int i = 1; i < count; i++) {
            if (money > max) {
                bound = money;
                bound = max;
            }
            double next = randomRedPacket(bound, 0.01, max, count);
            money = money - next;
            list.add(next);
        }
        list.add(money);
        return list;
    }

    public static void main(String args[]) {
        List list = new ArrayList();

        // 初始化
        Scanner scanner = new Scanner(System.in);
        System.out.println("------开始抢红包------");
        System.out.println("请输入红包的总金额（元）：");
        double sum = scanner.nextDouble();
        System.out.println("请输入红包的个数：");
        int count = scanner.nextInt();

        // 红包最小金额为0.01
        double min = 0.01;

        // 判断红包设置是否合理
        boolean b = isRight(sum, count);
        System.out.println(b);
        // 分红包
        if (b) {
            list = spiltRedPackets(sum, count);
        }
        // 遍历
        int i = 1;
        for (Object dou : list) {
            System.out.println("第" + (i++) + "个红包为：" + String.format("%.2f", dou) + "元");
        }
    }


    /**
     * 保证每人最少能分到0.01元
     * 且红包个数大于0
     *
     * @param money 红包总金额
     * @param count 红包总个数
     * @return
     */
    public static boolean isRight(double money, int count) {
        if (count < 1) {
            System.out.println("红包个数不能小于1！");
            return false;
        }
        double a = 0.01 * count;
        if (a < money) {
            System.out.println("正确！");
            return true;
        } else {
            System.out.println("金额太少！");
            return false;
        }
    }

    public static double randomRedPacket(double money, double minS, double maxS, int count) {
        Random abc = new Random();
        double safe = (double) abc.nextInt((int) (money * 100)) / 100;
        double next = safe + minS;/*本次红包的金额范围+每个红包的最小金额*/
        return next;
    }
}

